<?php

class loginController extends AppController
{

    public function loginAction()
    {

        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";
        $nameFlag = (!empty($this->params['rmp'])) ? $this->params['rmp'] : "";

        $session_validate = !empty($_SESSION["validate"]) ? $_SESSION["validate"] : '';

        if (!empty($_SESSION["zp_manage"]) && count($_SESSION["zp_manage"]) > 0 && $_SESSION['zp_manage']['islogin'] == true) {
            Tools::AlertTxt('index.php?md=manage&cl=index&at=home', '');
            return;
        }

        $this->view->setVariable("account", $account);
//        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function logincheckAction()
    {

        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";

        $session_validate = !empty($_SESSION['validate']) ?$_SESSION['validate'] : '';

        $ajax_result = array("status" => 0, "msg" => '');

        if (empty($account) || empty($pwd)) {
            $ajax_result["msg"] = '请输入帐号密码';
       // } else if (empty($validate)) {
        //    $ajax_result["msg"] = '请输入验证码';
        //} else if (strtolower($validate) != strtolower($session_validate)) {
        //    $ajax_result["msg"] = '验证码输入错误';
        } else {

            $rows = $this->model->loginCheck($account, $pwd);

//            echo '<pre>';
//            var_dump($rows);die;

            if (empty($rows) || $rows['isdeleted'] == 1) {
                $ajax_result["msg"] = '无此帐号';
            } else
                if (empty($rows["status"])) {
                    $ajax_result["msg"] = '请与管理者联系';
                } else

                    if ($rows['password'] == sha1($rows['salt'] . $pwd)) {
                        $_SESSION['zp_manage']['uid'] = $rows['id'];
                        $_SESSION['zp_manage']['name'] = $account;
                        $_SESSION['zp_manage']['islogin'] = true;
                        $_SESSION['login']['count'] = 0;

                        $result = $this->model->getItemById($rows['said'], 'systemallows');
                        $_SESSION['zp_manage']['permission'] = $result['permission'];
                        $_SESSION['zp_manage']['time'] = date('Y-m-d H:i', time());

                        $ajax_result["status"] = '1';
                        $ajax_result["msg"] = '登录成功';
                        $ajax_result["url"] = 'index.php?md=manage&cl=index&at=home';
                    } else {
                        $ajax_result["msg"] = '帐号或密码有误';
                    }
        }

        echo json_encode($ajax_result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    public function logoutAction()
    {
        if (isset($_SESSION['zp_manage']) && !empty($_SESSION['zp_manage'])) {
            unset($_SESSION['zp_manage']);
        }
        $this->loginAction();
    }


    public function hybriserrorAction()
    {
        $mail=new Mails();
        $to=array('1812534095@qq.com','1557769791@qq','546421169@qq');
        $subject='宅配系统通知－HYBRIS订单接口连线有误';
        $data=$subject.' 时间:'.date('Y-m-d H:i:s');
        $mail->sendMail($to[0], $subject, $data);

        $table='log_hybris_error';
        //新增
        $field=array();
        $field['type']='hybris';
        $field['message']='hybris接口连线失败';
        $field["isdeleted"] = 0;
        $field['createdt'] = 'now()';
        $r = $this->model->addItem($field, $table);
        exit;
    }

}

?>
